﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BCAMap.AMap;
using BCAMap.AMap.Dto;
using BCCommon.TMS.Transport;
using BCData.Common.Area;
using BCDto.Common.Area;
using BCDto.MallInterface;
using BCDto.TMS.Fence;
using BCDto.TMS.MallInterface;
using BCDto.TMS.Order.Order;
using BCEntity.Common.Area;
using BCService.TMS.Fence;
using BCService.TMS.MallInterface.OrderAsTo;
using BCService.TMS.Order.Order;
using BCService.TMS.TransportOrder;

namespace BCService.MallInterface
{
    public class RunErrandService : IRunErrandService
    {
        private IProvinceData provinceData;
        private ICityData cityData;
        private ICountyData countyData;
        private IFenceService fenceService;
        private readonly IAMapUtils iAMapUtils;
        private ITransportOrderService transportOrderService;
          
        MallOrderResposeDto  mallOrderResposeDto = new MallOrderResposeDto();

        #region 构造函数
        public RunErrandService(IProvinceData provinceData,
            ICityData cityData, ICountyData countyData,
            IFenceService fenceService,
            IAMapUtils iAMapUtils,
            ITransportOrderService transportOrderService)
        {
            this.provinceData = provinceData;
            this.cityData = cityData;
            this.countyData = countyData;
            this.fenceService = fenceService;
            this.iAMapUtils = iAMapUtils;
            this.transportOrderService = transportOrderService;
        }
        #endregion

        /// <summary>
        /// 跑腿费用计算接口
        /// </summary>
        /// <param name="mallOrderDto"></param> 
        /// <returns></returns>
        public MallOrderResposeDto GetResult(MallOrderDto mallOrderDto)
        {
            var sender = mallOrderDto.Sender;
            var receiver = mallOrderDto.Receiver;

            mallOrderResposeDto.TMSManageFeeBusinessType = BCCommon.TMS.ManageFeeBusiness.TMSManageFeeBusinessType.Horseman;
            mallOrderResposeDto.GoodsTotalWeight = mallOrderDto.GetTotalGoodsWeight();
            mallOrderResposeDto.GoodsTotalVolume = mallOrderDto.GetTotalGoodsVolume();
            mallOrderResposeDto.IsBeginCalcCost = false;
            mallOrderResposeDto.Result = false;
            mallOrderResposeDto.ResultDiscription = string.Empty;
            mallOrderResposeDto.Distance = 0;
            mallOrderResposeDto.vehicleTypeDto = null;

            if (sender.CountyCode != receiver.CountyCode)
            {
                mallOrderResposeDto.Result = false;
                mallOrderResposeDto.ResultDiscription = "不在同一个区县不能用跑腿!";
                return mallOrderResposeDto;
            }
            else
            {
                return GetRunErrandFence(mallOrderDto);
            }
        }

        #region 获取电动车骑行路径规划 
        public DirectionResponseDto GetDirectionResponseDto(double lng1, double lat1, double lng2, double lat2)
        {
            DirectionRequestDto directionRequestDto = new DirectionRequestDto();
            directionRequestDto.Origin = new double[2];
            directionRequestDto.Destination = new double[2];
            directionRequestDto.Origin[0] = lng1;
            directionRequestDto.Origin[1] = lat1;
            directionRequestDto.Destination[0] = lng2;
            directionRequestDto.Destination[1] = lat2;
            DirectionResponseDto dto = iAMapUtils.GetDirection(directionRequestDto);
            return dto;
        }
        #endregion

        //获取跑腿的围栏
        private MallOrderResposeDto GetRunErrandFence(MallOrderDto mallOrderDto)
        {
            var sender = mallOrderDto.Sender;
            var receiver = mallOrderDto.Receiver;

            CountyEntity SenderCountyEntity = countyData.GetCounty(sender.CountyCode);
            if (SenderCountyEntity == null)
            {
                mallOrderResposeDto.Result = false;
                mallOrderResposeDto.ResultDiscription = $"获取不到发货区县代码{sender.CountyCode}  {sender.CountyName}信息!";
                return mallOrderResposeDto;
            }

            var list = fenceService.GetIntoFenceList(sender.Lng, sender.Lat, SenderCountyEntity.ProvinceCode, SenderCountyEntity.CityCode, SenderCountyEntity.CountyCode, BCCommon.Enums.TMS.GpsMapType.GDMAP);
            if (list != null && list.Any())
            {
                if (fenceService.IsIntoFence(list[0].FenceId, receiver.Lng, receiver.Lat, BCCommon.Enums.TMS.GpsMapType.GDMAP))
                {
                    FenceDto fenceDto = list[0]; //这个是围栏信息
                    return GetDistance(mallOrderDto);
                }
                else
                {
                    mallOrderResposeDto.Result = false;
                    mallOrderResposeDto.ResultDiscription = $"目的地不在发货地的电子围栏内!";
                    return mallOrderResposeDto;
                }
            }
            else
            {
                mallOrderResposeDto.Result = false;
                mallOrderResposeDto.ResultDiscription = $"发货地不在(区县代码{sender.CountyCode}) {sender.CountyName}的电子围栏内!";
                return mallOrderResposeDto;
            }
        }

        private MallOrderResposeDto GetDistance(MallOrderDto mallOrderDto)
        {
            var sender = mallOrderDto.Sender;
            var receiver = mallOrderDto.Receiver;

            var dto = GetDirectionResponseDto(sender.Lng, sender.Lat, receiver.Lng, receiver.Lat);
            if (dto != null)
            {
                if (dto.status == "1")
                {
                    foreach (var item in dto.Route.Paths)
                    {
                        int len = 0;
                        if (int.TryParse(item.distance, out len))
                            mallOrderResposeDto.Distance += len;
                    }
                    Console.WriteLine($"高德距离:={mallOrderResposeDto.Distance}米");
                    ///////////////////////////////////////////////////////等着调用计费接口
                    var orderService = transportOrderService.GetOrderService(TransportBusinessType.RunErrands, null);
                    MallOrderTransf mallOrderTransf = new MallOrderTransf();
                    var orderShippingCostRequestDto =  mallOrderTransf.TransfCastOrder(mallOrderDto);
                    orderShippingCostRequestDto.TransportBusinessType = TransportBusinessType.RunErrands;
                    //    orderShippingCostRequestDto.VehicleCategoryReferenceId = 55;

                    mallOrderResposeDto.IsBeginCalcCost = true;
                    mallOrderResposeDto.orderCostChecklistDtos =  orderService.CalculateShippingCost(null,orderShippingCostRequestDto);
                    mallOrderResposeDto.Result = true;
                    return mallOrderResposeDto;
                }
                else
                {
                    mallOrderResposeDto.Result = false;
                    mallOrderResposeDto.ResultDiscription = $"获取高德路线规划失败,错误代码{dto.info}!";
                    return mallOrderResposeDto;
                }
            }
            else
            {
                mallOrderResposeDto.Result = false;
                mallOrderResposeDto.ResultDiscription = $"获取高德路线规划无返回!";
                return mallOrderResposeDto;
            }
        }

    }
     
}
